<?php
    function checkUnicode($str = '')
    {
        if(strlen($str) != strlen(removeUnicodeSpace($str, true)))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
    function removeUnicodeSpace($str = '', $removeSpace = false)
    {
        $result = "";
        
        //Loại bỏ dấu tiếng việt
        $unicode = array(
            'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ',
            'd'=>'đ',
            'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ',
            'i'=>'í|ì|ỉ|ĩ|ị',
            'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ',
            'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự',
            'y'=>'ý|ỳ|ỷ|ỹ|ỵ',
			'A'=>'Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
            'D'=>'Đ',
            'E'=>'É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
            'I'=>'Í|Ì|Ỉ|Ĩ|Ị',
            'O'=>'Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
            'U'=>'Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
            'Y'=>'Ý|Ỳ|Ỷ|Ỹ|Ỵ',
        );
        foreach($unicode as $nonUnicode=>$uni)
        {
            $str = preg_replace("/($uni)/i", $nonUnicode, $str);
        }
        
        //Xóa khoảng trắng
        if($removeSpace == true)
        {            
            $arr = explode(" ", $str);
            foreach($arr as $k=>$v)
                $result .= $v;
        }
        else
        {
            $result = $str;
        }
        
        return $result;
    }
    
    function chuyendoi_nam_thang_ngay($str,$split,$preg){
        //note: output: dd/mm/yyyy
        //convert 13/12/2012 --> 13-12-2012
        $arr = explode($split, $str);
        $ngay = (int)$arr[0];
		$thang = (int)$arr[1];
		$nam = (int)$arr[2];

        if((int)$arr[0]<10)
            $ngay = "0".$ngay;
        if((int)$arr[1]<10)
            $thang = "0".$thang;

        $yyyy_mm_dd = $nam.$preg.$thang.$preg.$ngay;
        return $yyyy_mm_dd;
    }
    
    function hienthi_ngay_thang_nam($str)
    {
        $ngay_thang_nam = date_create($str);
        return date_format($ngay_thang_nam, 'd-m-Y');
    }
    
    function kiemtra_dulieurong()
    {
        if(count($_POST)==0)
            return true;
        else
            return false;
    }
    function catchuoi($str, $length, $minword = 3)
    {
        $sub = '';
        $len = 0;
        foreach (explode(' ', $str) as $word)
        {
            $part = (($sub != '') ? ' ' : '') . $word;
            $sub .= $part;
            $len += strlen($part);
            if (strlen($word) > $minword && strlen($sub) >= $length)
            {
              break;
            }
         }
        return $sub . (($len < strlen($str)) ? '...' : '');
    }
    
    function catcau($str,$limit=10)
    {
        if(stripos($str," "))
        {
            $ex_str = explode(" ",$str);
            if(count($ex_str)>$limit)
            {
               for($i=0;$i<$limit;$i++)
               {
                   $str_s.=$ex_str[$i]." ";
               }
               return $str_s;
            }
            else
            {
               return $str;
            }
        }
        else
        {
               return $str;
        }
    }
?>